package com.zwg.git_customview.mp4

import android.os.Bundle
import android.view.SurfaceView
import androidx.appcompat.app.AppCompatActivity
import com.zwg.git_customview.R


class VideoPlayerActivity : AppCompatActivity() {

    private lateinit var mediaPlayerHelper: MediaPlayerHelper

    lateinit var surfaceView: SurfaceView  // 视频播放的SurfaceView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_surfaceview)
        surfaceView = findViewById<SurfaceView>(R.id.surfaceView)

        mediaPlayerHelper = MediaPlayerHelper(this, surfaceView)
        mediaPlayerHelper.playVideo()
    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayerHelper.surfaceDestroyed(surfaceView.holder)
    }
}

//// VideoPlayerActivity.kt
//import android.media.MediaPlayer
//import android.net.Uri
//import android.os.Bundle
//import android.view.SurfaceHolder
//import android.view.SurfaceView
//import androidx.appcompat.app.AppCompatActivity
//import com.zwg.git_customview.R

//class VideoPlayerActivity : AppCompatActivity(), SurfaceHolder.Callback {
//
//    private lateinit var mediaPlayer: MediaPlayer
//    private lateinit var surfaceHolder: SurfaceHolder
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_videoplay)
//
//        val surfaceView = findViewById<SurfaceView>(R.id.surfaceView)
//        surfaceHolder = surfaceView.holder
//        surfaceHolder.addCallback(this)
//    }
//
//    override fun surfaceCreated(holder: SurfaceHolder) {
//        mediaPlayer = MediaPlayer().apply {
//            setDisplay(holder)
//            val rawVideoId = R.raw.login_bg
//            val uri = "android.resource://${packageName}/${rawVideoId}"
//            setDataSource(applicationContext, Uri.parse(uri))
//            setOnPreparedListener {
//                start()
//            }
//            setOnCompletionListener {
//                // 循环播放
//                it.start()
//            }
//            prepareAsync()
//        }
//    }
//
//    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
//
//    }
//
//    override fun surfaceDestroyed(holder: SurfaceHolder) {
//        mediaPlayer.run {
//            if (isPlaying) stop()
//            release()
//        }
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        mediaPlayer.release()
//    }
//}
